/*
  User: Yemao Luo
  Date: 2021/10/15
  Time: 13:59
*/

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Class05HW {
    public static void main(String[] args) {
        //大件
        equipment 死亡之刃 = new equipment("死亡之刃");
        equipment 巨人杀手 = new equipment("巨人杀手");
        equipment 海克斯科技枪 = new equipment("海克斯科技枪");
        equipment 朔极之矛 = new equipment("朔极之矛");
        equipment 守护天使 = new equipment("守护天使");
        equipment 饮血剑 = new equipment("饮血剑");
        equipment 基克的先驱 = new equipment("基克的先驱");
        equipment 破败王者之刃 = new equipment("破败王者之刃");

        //中件
        equipment 反曲弓 = new equipment("反曲弓");
        equipment 无用大棒 = new equipment("无用大棒");
        equipment 女神之泪 = new equipment("女神之泪");
        equipment 锁子甲 = new equipment("锁子甲");
        equipment 巨人腰带 = new equipment("巨人腰带");
        equipment 魔法斗篷 = new equipment("魔法斗篷");

        //小件
        equipment 暴风大剑 = new equipment("暴风大剑");
        equipment 金铲子 = new equipment("金铲子");
        equipment 红水晶 = new equipment("红水晶");
        equipment 布甲 = new equipment("布甲");
        equipment 攻速刀 = new equipment("攻速刀");
        equipment 蓝水晶 = new equipment("蓝水晶");
        equipment 魔抗披风 = new equipment("魔抗披风");
        equipment 魔法书 = new equipment("魔法书");

        List<equipment> allEquipment = new ArrayList<>();
        allEquipment.add(死亡之刃);
        allEquipment.add(巨人杀手);
        allEquipment.add(海克斯科技枪);
        allEquipment.add(朔极之矛);
        allEquipment.add(守护天使);
        allEquipment.add(饮血剑);
        allEquipment.add(基克的先驱);
        allEquipment.add(破败王者之刃);
        allEquipment.add(反曲弓);
        allEquipment.add(无用大棒);
        allEquipment.add(女神之泪);
        allEquipment.add(锁子甲);
        allEquipment.add(巨人腰带);
        allEquipment.add(魔法斗篷);
        allEquipment.add(暴风大剑);
        allEquipment.add(金铲子);
        allEquipment.add(红水晶);
        allEquipment.add(布甲);
        allEquipment.add(攻速刀);
        allEquipment.add(蓝水晶);
        allEquipment.add(魔抗披风);
        allEquipment.add(魔法书);

        //设置关系
        蓝水晶.setParentNodes(女神之泪);
        女神之泪.setChildNodes(蓝水晶);
        攻速刀.setParentNodes(反曲弓);
        反曲弓.setChildNodes(攻速刀);
        布甲.setParentNodes(锁子甲);
        锁子甲.setChildNodes(布甲);
        红水晶.setParentNodes(巨人腰带);
        巨人腰带.setChildNodes(红水晶);
        金铲子.setParentNodes(破败王者之刃);
        破败王者之刃.setChildNodes(金铲子);
        暴风大剑.setParentNodes(死亡之刃);
        死亡之刃.setChildNodes(暴风大剑);
        死亡之刃.setChildNodes(暴风大剑);
        暴风大剑.setParentNodes(巨人杀手);
        巨人杀手.setChildNodes(暴风大剑);
        巨人杀手.setChildNodes(反曲弓);
        反曲弓.setParentNodes(巨人杀手);
        暴风大剑.setParentNodes(海克斯科技枪);
        海克斯科技枪.setChildNodes(暴风大剑);
        海克斯科技枪.setChildNodes(无用大棒);
        无用大棒.setParentNodes(海克斯科技枪);
        暴风大剑.setParentNodes(朔极之矛);
        朔极之矛.setChildNodes(暴风大剑);
        朔极之矛.setChildNodes(女神之泪);
        女神之泪.setParentNodes(朔极之矛);
        暴风大剑.setParentNodes(守护天使);
        守护天使.setChildNodes(暴风大剑);
        守护天使.setChildNodes(锁子甲);
        暴风大剑.setParentNodes(饮血剑);
        饮血剑.setChildNodes(暴风大剑);
        饮血剑.setChildNodes(魔法斗篷);
        暴风大剑.setParentNodes(基克的先驱);
        基克的先驱.setChildNodes(暴风大剑);
        基克的先驱.setChildNodes(巨人腰带);
        暴风大剑.setParentNodes(破败王者之刃);
        破败王者之刃.setChildNodes(暴风大剑);
        破败王者之刃.setChildNodes(金铲子);
        魔抗披风.setParentNodes(魔法斗篷);
        魔法斗篷.setChildNodes(魔抗披风);
        魔法书.setParentNodes(无用大棒);
        无用大棒.setChildNodes(魔法书);

        //Driver
        System.out.print("需要查询的装备为:");
        Scanner sc = new Scanner(System.in);
        String equipmentName = sc.next();
        int No = -1;
        for (int i = 0; i < allEquipment.size(); i++) {
            if (allEquipment.get(i).getName().equals(equipmentName)){
                No = i;
                break;
            }
        }
        if (No == -1){
            System.out.println("输入装备不存在");
            return;
        }else {
            equipment equipmentSelected = allEquipment.get(No);
            System.out.print("合成此装备需要:");
            if (equipmentSelected.getChildNodes() == null){
                System.out.println("空");
            }else {
                equipmentSelected.traverseChildPointsPrint();
            }
            System.out.print("此装备可以合成:");
            if (equipmentSelected.getParentNodes() == null){
                System.out.println("空");
            }else {
                equipmentSelected.traverseParentPointsPrint();
            }
        }
    }
}

class equipment{
    private List<equipment> parentNodes;
    private List<equipment> childNodes;
    private String name;

    public equipment(String name) {
        this.name = name;
    }

    public void setParentNodes(equipment equipment) {
        List<equipment> newParentNodes = new ArrayList<>();
        if (this.parentNodes == null){
            newParentNodes.add(equipment);
            this.parentNodes = newParentNodes;
            return;
        }
        for (int i = 0; i < this.parentNodes.size(); i++) {
            newParentNodes.add(this.parentNodes.get(i));
        }
        newParentNodes.add(equipment);
        this.parentNodes = newParentNodes;
    }

    public void setChildNodes(equipment equipment) {
        List<equipment> newChildNodes = new ArrayList<>();
        if (this.childNodes == null){
            newChildNodes.add(equipment);
            this.childNodes = newChildNodes;
            return;
        }
        for (int i = 0; i < this.childNodes.size(); i++) {
            newChildNodes.add(this.childNodes.get(i));
        }
        newChildNodes.add(equipment);
        this.childNodes = newChildNodes;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return name;
    }

    public List<equipment> getParentNodes() {
        return parentNodes;
    }

    public List<equipment> getChildNodes() {
        return childNodes;
    }
    
    public void traverseChildPointsPrint(){
        List<equipment> equipmentList = new ArrayList<>();
        for (int i = 0; i < this.getChildNodes().size(); i++) {
            equipmentList.add(this.getChildNodes().get(i));
        }
        for (int i = 0; i < equipmentList.size(); i++) {
            equipment equipment = equipmentList.get(i);
            if (equipment.getChildNodes() == null){
                continue;
            }else {
                for (int j = 0; j < equipment.getChildNodes().size(); j++) {
                    equipmentList.add(equipment.getChildNodes().get(j));
                }
            }
        }
        System.out.println(equipmentList.toString());
    }

    public void traverseParentPointsPrint(){
        List<equipment> equipmentList = new ArrayList<>();
        for (int i = 0; i < this.getParentNodes().size(); i++) {
            equipmentList.add(this.getParentNodes().get(i));
        }
        for (int i = 0; i < equipmentList.size(); i++) {
            equipment equipment = equipmentList.get(i);
            if (equipment.getParentNodes() == null){
                continue;
            }else {
                for (int j = 0; j < equipment.getParentNodes().size(); j++) {
                    equipmentList.add(equipment.getParentNodes().get(j));
                }
            }
        }
        System.out.println(equipmentList.toString());
    }
}
